<?php
/* coded by e1r0nd 05.03.12 */
/* Placed in public domain      */
class Quote {

	private function XML_Header_printout () {
		header("Content-Type: text/xml; charset=utf-8");
		echo'<?xml version="1.0" encoding="utf-8"?>'."\r\n".'<result>'."\r\n";
	}

	private function XML_Footer_printout () {
		echo'</result>';
	}

	private function XML_Quote_printout ($r) {
		echo '<quote>'."\r\n";
		echo '<id>'.$r->id.'</id>'."\r\n";
		echo '<title>'.$r->title.'</title>'."\r\n";
		echo '<author>'.$r->author.'</author>'."\r\n";
		echo '<content>'.$r->content.'</content>'."\r\n";
		echo '</quote>'."\r\n";
	}

	public function XML_Quote_view ($uid) {
		$quotes = classDB::getInstance()->DB_read('*', 'Quotes', 'uid', (int) $uid, '', 'id', 'DESC');
		$this->XML_Header_printout ();
		while ($r = mysql_fetch_object($quotes)) {
			$this->XML_Quote_printout($r);
		}
		$this->XML_Footer_printout ();
	}
	
	public function Quote_delete ($id) {
		if ( (int) $id ) {
			classDB::getInstance()->DB_remove('Quotes', $id);
		}
	}
	
	public function Quote_edit ($id, $title, $author, $content) {
		$id = (int) $id;
		$title = preg_replace ("/[^0-9a-zA-Zа-яА-ЯёЁыЫцушщрэчтьюхъф .,-:()\s]/","",$title);
		$title = substr($title, 0, 99);
		$author = preg_replace ("/[^0-9a-zA-Zа-яА-ЯёЁыЫцушщрэчтьюхъф .,-:()\s]/","",$author);
		$author = substr($author, 0, 50);
		$content = preg_replace ("/[^0-9a-zA-Zа-яА-ЯёЁыЫцушщрэчтьюхъф .,-:?!;()\s]/","",$content);
		$content = substr($content, 0, 250);
	
		classDB::getInstance()->DB_update('Quotes', "`title` = '$title', `author` = '$author', `content` = '$content'", $id);

		echo $id;
	}
	
	public function Quote_add ($uid, $title, $author, $content) {
		$uid = (int) $uid;
		$title = preg_replace ("/[^0-9a-zA-Zа-яА-ЯёЁыЫцушщрэчтьюхъф .,-:()\s]/","",$title);
		$title = substr($title, 0, 99);
		$author = preg_replace ("/[^0-9a-zA-Zа-яА-ЯёЁыЫцушщрэчтьюхъф .,-:()\s]/","",$author);
		$author = substr($author, 0, 50);
		$content = preg_replace ("/[^0-9a-zA-Zа-яА-ЯёЁыЫцушщрэчтьюхъф .,-:?!;()\s]/","",$content);
		$content = substr($content, 0, 250);
			
		classDB::getInstance()->DB_insert('Quotes', "`uid`, `id`, `title`, `author`, `content`", "'$uid', NULL, '$title', '$author', '$content'");
	
		$quotes = classDB::getInstance()->DB_read('id', 'Quotes', '1', '1', '', 'id', 'DESC LIMIT 1');
		$r = mysql_fetch_object($quotes);
		echo $r->id;
	}

}